package main

import (
	"context"
	"fmt"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"log"
	"math/big"
)

func GetBalance(ethAddress string) (balance1 *big.Float) {
	// 连接到以太坊节点
	client, err := ethclient.Dial("https://bsc-dataseed.bnbchain.org/")
	if err != nil {
		log.Fatalf("Failed to connect to the Ethereum client: %v", err)
	}

	// 查询的以太坊地址
	//address := common.HexToAddress("0xf5878dd0b16bb4045406c36381486aa9dbc09161") // 替换为你的以太坊地址
	//address := common.HexToAddress("0xd00107db27f2d8952a736f5d2133354f76e19235") // 替换为你的以太坊地址
	address := common.HexToAddress(ethAddress) // 替换为你的以太坊地址

	// 获取账户余额
	balance, err := client.BalanceAt(context.Background(), address, nil)
	if err != nil {
		fmt.Println("Failed to get balance", err)
		return
	}

	// 将余额转换为 Ether 单位
	balanceInEther := new(big.Float).Quo(new(big.Float).SetInt(balance), new(big.Float).SetInt(big.NewInt(1e18)))

	fmt.Printf("Balance of %s: %v BNB\n", address.Hex(), balanceInEther)

	return balanceInEther
}
